const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

exports.main = async (event, context) => {
  try {
    // 检查是否已存在默认用户
    const userCheck = await db.collection('users').where({
      username: 'admin'
    }).get()
    
    if (userCheck.data.length === 0) {
      // 创建默认管理员用户
      await db.collection('users').add({
        data: {
          username: 'admin',
          password: '123456',  // 实际应用中应该使用加密密码
          role: 'admin',
          createTime: db.serverDate()
        }
      })
    }
    
    return {
      success: true,
      message: '初始化用户成功'
    }
  } catch (error) {
    console.error(error)
    return {
      success: false,
      message: '初始化用户失败'
    }
  }
} 